﻿package  {
	
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.display.*;
	import flash.net.URLRequest;
	
	public class TObject extends Sprite {
		private var _Mass: Number = 0;
		private var _bmp: Bitmap;
		private var _Loading: Boolean = false;
		private var _Func: Function = null;
		private var _Active: Boolean = true;
		
		public var isDragging = false;

		public function TObject(Mass: Number, X: int, Y: int, Height: int, Width: int, bmpData: BitmapData) {		
			
			super();
			
			if (!(bmpData == null)) {
				_bmp = new Bitmap(bmpData, "auto", true);
				_bmp.x = 0;
				_bmp.y = 0;
				_bmp.height = Height;
				_bmp.width = Width;
				this.addChild(_bmp);
			} else {
				_bmp = new Bitmap();
				this.addChild(_bmp);
			}

			this.Mass = Mass;
			this.x = X;
			this.y = Y;
			this.height = Height;
			this.width = Width;
		}

		
		public function set Mass(Value: Number) {
			if (Value >= 0) {
				_Mass = Value;
			} else {
				_Mass = 0;
			}
		}
		
		public function set BmpData(Value: BitmapData) {
			this.removeChild(_bmp);
			if (!(Value == null)) {
				_bmp = new Bitmap(Value, "auto", true);
				_bmp.x = 0;
				_bmp.y = 0;
				_bmp.height = height;
				_bmp.width = width;
				this.addChild(_bmp);
			} else {
				_bmp = new Bitmap();
				this.addChild(_bmp);
			}
		}
		
		public function set Bmp(Value: Bitmap) {
			this.removeChild(_bmp);
			_bmp = Value;
			if (!(_bmp == null)) {
				this.addChild(_bmp);
			} else {
				_bmp = new Bitmap();
				this.addChild(_bmp);
			}
		}
		
		public function set Active(Value: Boolean) {
			_Active = Value;
		}
		
		public function get Mass(): Number { return _Mass };
		public function get Bmp(): Bitmap { return _bmp };
		
		public function get isMoving(): Boolean {
			return isDragging;
		}
		
		public function get Active(): Boolean {
			return _Active;
		}
		
		public function Move(X, Y: int) {
			this.x = this.x + X;
			this.y = this.y + Y;
		}
		
		override public function toString(): String {
			return "Mass: " + Mass + "; X: " + x + "; Y: " + y + "; Height: " + 
				height + "; Width: " + width + "; " + Bmp + ";";
		}
		
		public function LoadSprite(URL: String, Func: Function = null) {
			var loader: Loader = new Loader();
			
			_Func = Func;
    		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    		loader.load(new URLRequest(URL));
		}
		
		private function onComplete(event: Event) {
			this.BmpData = Bitmap(LoaderInfo(event.target).content).bitmapData;
			if (!(_Func == null)) {
				_Func(this);
			}
		}

	}
	
}
